<?php
namespace core\lib\driver\database;

class mysqli
{
	private $db;
	
	/**
	 * 
	 *	构造函数生成mysqli对象
	 *
	 */
	public function __construct($host, $user, $pass, $dbname, $port=3306){
		$this->db = new \mysqli($host, $user, $pass, $dbname, $port);
	}
	
	/**
	 * 
	 *	更新数据成功，返回结果集二维Array(关联数组)，每一行代表一个记录
	 * 	否则返回False
	 *
	 * @return	false Or Array
	 */
	 
	public function query($sql){
		$query = $this->db->query($sql);
		if(!$query)
			return false;
		$result = array();
		while($res = $query->fetch_assoc()){
			$result[] = $res;
		}
		return $result;
	}
	
	/**
	 * 
	 *	更新数据成功，返回True
	 * 	否则返回False
	 *
	 * @return	Boolean
	 */
	public function update($sql){
		$ok = $this->db->query($sql);
		return $ok;
	}
	
	/**
	 *	返回插入成功的id
	 */
	public function insert_id(){
		return $this->db->insert_id;
	}
	/**
	 * 
	 *	如果连接数据库成功，则返回false
	 * 	否则，返回错误信息
	 *
	 * @return	false or String
	 */
	public function connect_error(){
		if($this->db->connect_error)
			return "Connect Eorror({$this->db->connect_errno}): {$this->db->connect_error}";
		return false;
	}
	
	/**
	 * 
	 *	如果执行SQL代码成功，则返回false
	 * 	否则，返回错误信息
	 *
	 * @return	false or String
	 */
	public function error(){
		if($this->db->error)
			return "Error ({$this->db->errno}): {$this->db->error}";
		return false;
	}
}